package com.superm.controller;

import com.superm.entity.*;
import com.superm.service.PermissionService;
import com.superm.service.RolePerService;
import com.superm.service.RoleService;
import com.superm.utils.EmpContent;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(path = "/permission")
public class PermissionController {
    @Autowired
    private PermissionService permissionService;
    @Autowired
    private RolePerService rolePerService;
    @Autowired
    private RoleService roleService;

    public List<Integer> GOG(){//公共方法
        Emp emp = EmpContent.EmpUtils();
        //查询一级菜单
        List<Integer> roles = new ArrayList<Integer>();
        emp.getRoles().forEach(role ->{
            roles.add(role.getRid());
        });
        return roles;
    }
    @RequestMapping(path = "/per")
    public ModelAndView per(int id){
        ModelAndView ma = new ModelAndView();
        Role role = roleService.queryId(id);
        List<RolePermission> pers  = roleService.allPerId(id);//根据id查询出来所有权限
        List<Integer> perids =new ArrayList<>();
        for (RolePermission rp: pers) {
            perids.add(rp.getPerid());
        }
        ma.addObject("perids",perids);
        ma.addObject("role",role);
        ma.setViewName("permission/permission");
        return ma;
    }

    @RequestMapping( "/perAll")
    @ResponseBody   //权限展示
    public Object perAll(){
        Emp emp = EmpContent.EmpUtils();
        //查询一级菜单下的子菜单
        List<Permission> perS = permissionService.AllperId(GOG(),emp.geteId());
        return perS;
    }
    @RequestMapping("/getQuery")
    @ResponseBody
    public List<Permission> getQuery(){
        List<Permission> perS = permissionService.AllperId1();
        return perS;
    }
    @RequestMapping(path = "/updatePerId")
    @ResponseBody//角色授权设置
    public Map<String,Object> updatePerId(@RequestParam("arrperId[]") Integer[] arrperId,@RequestParam("idStr")String idStr){
        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        Integer id =Integer.parseInt(idStr);
        System.out.println(id);
        System.out.println(arrperId.length);
        map.put("success",false);
        map.put("msg","修改失败!");
        List<RolePermission> rps = new ArrayList<RolePermission>();
        RolePermission rp = null;
        for (int i=0;i<arrperId.length;i++){
            rp = new RolePermission();
            rp.setPerid(arrperId[i]);
            rp.setRoleid(id);
            rps.add(rp);
        }
        rolePerService.addPer(id,rps);
            map.put("success",true);
            map.put("msg","修改成功!");
        return map;
    }
}